package com.flyox.game.fivetiger.view;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.flyox.game.fivetiger.bean.CONSTS;
import com.flyox.game.fivetiger.bean.MsgPackage;
import com.flyox.game.fivetiger.bean.Position;
import com.flyox.game.fivetiger.control.FivetigerControl;
import com.flyox.game.fivetiger.tools.UtilBean;

/**
 * 棋盘面板
 * 
 * @author sunwei
 * 
 */
public class LayoutJPanel extends javax.swing.JPanel implements MouseListener {

	private static final long serialVersionUID = -4944195455767656223L;

	public Position selectedChessMan;// 第一次点击选中的棋子 行 列
	FivetigerControl control;
	public ChessManLabel[][] cml = new ChessManLabel[5][5];
	UtilBean utilBean;
	BufferedImage fiveImage = null;

	public LayoutJPanel(FivetigerControl control) {
		this.control = control;
		try {
			fiveImage = ImageIO.read(this.getClass().getResource("/com/flyox/game/fivetiger/res/five.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		initGUI();
		utilBean = new UtilBean();
	}

	private void initGUI() {
		this.setLayout(null);
		addMouseListener(this);
		setSize(600, 600);
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(fiveImage, 0, 0, this);
		// this.setOpaque( false );//不透明物
	}

	public void reDrawLayout() {
		this.removeAll();
		int[][] layout = control.getDoc().getLayout();
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				if (layout[i][j] == CONSTS.CHESS_EMPTY) {
					continue;
				}
				cml[i][j] = new ChessManLabel(new Position(i, j), control);
				cml[i][j].setBounds(utilBean.getOffset(j), utilBean.getOffset(i), utilBean.CHESS_WIDTH, utilBean.CHESS_WIDTH);
				this.add(cml[i][j]);
			}
		}
		this.repaint();
	}

	/**
	 * 处理下棋动作
	 * 
	 * @param pos
	 */
	public void processAddClick(Position pos) {
		if (control.getDoc().getLayout()[pos.row][pos.col] != CONSTS.CHESS_EMPTY) {
			return;
		}
		// 修改布局 需要检查是否填满棋子，是否成势 拔子
		int beRemove = control.getDoc().addCurrenPlayerChess(pos);

		if (beRemove > 0 && !utilBean.haveAnyChessRemove(control.getDoc().getLayout(), control.getDoc().getOtherSideTeamFlag())) {
			beRemove = 0;
		}

		// 发送信息给对方
		MsgPackage msgPackage = new MsgPackage();
		msgPackage.setCmd(CONSTS.CMD_ADD);
		msgPackage.setCmdPlayer(control.getDoc().getCurrentPlayer());
		msgPackage.setFirstPos(pos);
		if (beRemove > 0) {
			msgPackage.setCurrentState(CONSTS.CMD_COVER);
			msgPackage.setCurrentPlayer(control.getDoc().getName());
			msgPackage.setCanRemoveChessCount(beRemove);
		} else if (control.getDoc().isFull()) {// remove all cover
			msgPackage.setCurrentState(CONSTS.CMD_MOVE);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
			msgPackage.cloneFullLayout(control.getDoc().getLayout());
			control.getDoc().removeAllCovered();
		} else {
			msgPackage.setCurrentState(CONSTS.CMD_ADD);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
		}
		control.sendMsg(msgPackage);

		// 修改本地doc
		control.getDoc().pushStept(msgPackage);
		control.getDoc().setCurrentPlayer(msgPackage.getCurrentPlayer());
		control.getDoc().setCurrentState(msgPackage.getCurrentState());
		control.getDoc().setCanRemoveChessCount(beRemove);
	}

	/**
	 * 处理走棋动作
	 * 
	 * @param pos
	 */
	public void processMoveClick(Position pos) {
		if (selectedChessMan == null) {
			return;
		}

		// 是否能够移动到这里
		if (!utilBean.chessCanMoveHereToHere(selectedChessMan, pos)) {
			System.out.println("不能移动from=" + selectedChessMan.toString() + " to=" + pos.toString());
			return;
		}

		// 修改doc 布局，是否成势 拔子
		int canRemoveCount = control.getDoc().move(selectedChessMan, pos);
		if (canRemoveCount > 0 && !utilBean.haveAnyChessRemove(control.getDoc().getLayout(), control.getDoc().getOtherSideTeamFlag())) {
			canRemoveCount = 0;
		}
		// 发送信息给对方
		MsgPackage msgPackage = new MsgPackage();
		msgPackage.setCmd(CONSTS.CMD_MOVE);
		msgPackage.setCmdPlayer(control.getDoc().getCurrentPlayer());
		msgPackage.setFirstPos(selectedChessMan);
		msgPackage.setSecondPos(pos);
		if (canRemoveCount > 0) {
			msgPackage.setCurrentState(CONSTS.CMD_CUT);
			msgPackage.setCurrentPlayer(control.getDoc().getName());
			msgPackage.setCanRemoveChessCount(canRemoveCount);
		} else {
			msgPackage.setCurrentState(CONSTS.CMD_MOVE);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
		}
		control.sendMsg(msgPackage);

		// 修改本地doc
		control.getDoc().pushStept(msgPackage);
		control.getDoc().setCurrentPlayer(msgPackage.getCurrentPlayer());
		control.getDoc().setCurrentState(msgPackage.getCurrentState());
		control.getDoc().setCanRemoveChessCount(canRemoveCount);
		selectedChessMan = null;
	}

	public void mouseClicked(MouseEvent e) {
		// 不是当前操作用户点击无效
		if (!control.getDoc().isCurrentPlayerPlay()) {
			return;
		}

		// 取得棋盘坐标
		Position pos = utilBean.posToRowCol(e.getX(), e.getY());
		if (pos == null) {
			return;
		}
		switch (control.getDoc().getCurrentState()) {
		case CONSTS.CMD_ADD:
			processAddClick(pos);
			break;
		case CONSTS.CMD_MOVE:
			processMoveClick(pos);
			break;
		default:
			break;
		}
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}
}
